home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3.iso / chapter8 / wprvstr.c < prev    next >
C/C++ Source or Header  |  1996-04-28  |  7KB  |  217 lines

  1.  
  2. #include <windows.h>  
  3. #include <stdio.h>
  4. #include "wprvstr.h"
  5. #include "sqlext.h"  
  6. #include "odbcinst.h"  
  7.  
  8.  
  9. #if defined (WIN32)
  10.     #define IS_WIN32 TRUE
  11. #else
  12.     #define IS_WIN32 FALSE
  13. #endif
  14.  
  15. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  16. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  17. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  18.  
  19. HINSTANCE hInst;   // current instance
  20.  
  21. LPCTSTR lpszAppName = "MyApp";
  22. LPCTSTR lpszTitle   = "SQLWritePrivateProfileString()"; 
  23.  
  24.  
  25. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  26.  
  27.  
  28. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  29.                       LPTSTR lpCmdLine, int nCmdShow)
  30. {
  31.    MSG      msg;
  32.    HWND     hWnd; 
  33.    WNDCLASS wc;
  34.  
  35.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  36.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  37.    wc.cbClsExtra    = 0;                      
  38.    wc.cbWndExtra    = 0;                      
  39.    wc.hInstance     = hInstance;              
  40.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  41.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  42.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  43.    wc.lpszMenuName  = lpszAppName;              
  44.    wc.lpszClassName = lpszAppName;              
  45.  
  46.    if ( IS_WIN95 )
  47.    {
  48.       if ( !RegisterWin95( &wc ) )
  49.          return( FALSE );
  50.    }
  51.    else if ( !RegisterClass( &wc ) )
  52.       return( FALSE );
  53.  
  54.    hInst = hInstance; 
  55.  
  56.    hWnd = CreateWindow( lpszAppName, 
  57.                         lpszTitle,    
  58.                         WS_OVERLAPPEDWINDOW, 
  59.                         CW_USEDEFAULT, 0, 
  60.                         CW_USEDEFAULT, 0,  
  61.                         NULL,              
  62.                         NULL,              
  63.                         hInstance,         
  64.                         NULL               
  65.                       );
  66.  
  67.    if ( !hWnd ) 
  68.       return( FALSE );
  69.  
  70.    ShowWindow( hWnd, nCmdShow ); 
  71.    UpdateWindow( hWnd );         
  72.  
  73.    while( GetMessage( &msg, NULL, 0, 0) )   
  74.    {
  75.       TranslateMessage( &msg ); 
  76.       DispatchMessage( &msg );  
  77.    }
  78.  
  79.    return( msg.wParam ); 
  80. }
  81.  
  82.  
  83. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  84. {
  85.    WNDCLASSEX wcex;
  86.  
  87.    wcex.style         = lpwc->style;
  88.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  89.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  90.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  91.    wcex.hInstance     = lpwc->hInstance;
  92.    wcex.hIcon         = lpwc->hIcon;
  93.    wcex.hCursor       = lpwc->hCursor;
  94.    wcex.hbrBackground = lpwc->hbrBackground;
  95.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  96.    wcex.lpszClassName = lpwc->lpszClassName;
  97.  
  98.    // Added elements for Windows 95.
  99.    //...............................
  100.    wcex.cbSize = sizeof(WNDCLASSEX);
  101.    wcex.hIconSm = LoadIcon(wcex.hInstance, "SMALL");
  102.             
  103.    return RegisterClassEx( &wcex );  
  104. }
  105.  
  106.  
  107. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  108. {
  109. static HWND hList = NULL;
  110.  
  111.    switch( uMsg )
  112.    {
  113.       case WM_CREATE :
  114.               {
  115.                  hList = CreateWindow( "LISTBOX", "",    
  116.                                        LBS_NOTIFY | WS_VSCROLL | 
  117.                                        WS_BORDER  | WS_CHILD | 
  118.                                        WS_VISIBLE | LBS_NOINTEGRALHEIGHT, 
  119.                                        0, 0, 
  120.                                        0, 0,  
  121.                                        hWnd,              
  122.                                        (HMENU)101,              
  123.                                        hInst,         
  124.                                        NULL               
  125.                                      );
  126.               }
  127.               break;
  128.  
  129.       case WM_SIZE :
  130.               MoveWindow( hList, 0, 0, LOWORD( lParam ), 
  131.                                        HIWORD( lParam ), TRUE );
  132.               break; 
  133.               
  134.       case WM_COMMAND :
  135.               switch( LOWORD( wParam ) )
  136.               {
  137.                  case IDM_TEST :
  138.                         {
  139.                            DWORD dRet;
  140.                            UCHAR szLBStr[128];
  141.  
  142.                            // Call SQLWritePrivateProfileString() to add
  143.                            // a default password to "New Data Source".
  144.                            // ..........................................
  145.                            dRet = SQLWritePrivateProfileString(
  146.                                     "New Data Source",
  147.                                     "PWD",
  148.                                     "Admin",
  149.                                     "ODBC.INI" );
  150.  
  151.                            strcpy( szLBStr, 
  152.                                    "SQLWritePrivateProfileString() " );
  153.  
  154.                            // Display status to the user.
  155.                            // ...........................
  156.                            if( dRet )
  157.                            {
  158.                               strcat( szLBStr, "successful" );
  159.  
  160.                               SendMessage( hList, LB_ADDSTRING, 0, 
  161.                                           (LPARAM)szLBStr );
  162.                            }
  163.                            else
  164.                            {
  165.                               strcat( szLBStr, "not successful" );
  166.  
  167.                               SendMessage( hList, LB_ADDSTRING, 0, 
  168.                                           (LPARAM)szLBStr );
  169.                            }
  170.                         }
  171.                         break;
  172.  
  173.                  case IDM_ABOUT :
  174.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  175.                         break;
  176.  
  177.                  case IDM_EXIT :
  178.                         DestroyWindow( hWnd );
  179.                         break;
  180.               }
  181.               break;
  182.       
  183.       case WM_DESTROY :
  184.               PostQuitMessage(0);
  185.               break;
  186.  
  187.       default :
  188.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  189.    }
  190.  
  191.    return( 0L );
  192. }
  193.  
  194.  
  195. LRESULT CALLBACK About( HWND hDlg,           
  196.                         UINT message,        
  197.                         WPARAM wParam,       
  198.                         LPARAM lParam)
  199. {
  200.    switch (message) 
  201.    {
  202.        case WM_INITDIALOG: 
  203.                return (TRUE);
  204.  
  205.        case WM_COMMAND:                              
  206.                if (   LOWORD(wParam) == IDOK         
  207.                    || LOWORD(wParam) == IDCANCEL)    
  208.                {
  209.                        EndDialog(hDlg, TRUE);        
  210.                        return (TRUE);
  211.                }
  212.                break;
  213.    }
  214.  
  215.    return (FALSE); 
  216. }
  217.